package com.ihcil.forecast.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ihcil.forecast.demo.mapper.RadarMapper;
import com.ihcil.forecast.demo.pojo.Radar;
import com.ihcil.forecast.demo.pojo.User;
import com.ihcil.forecast.demo.service.RadarService;
import com.ihcil.forecast.demo.utils.Str2Time;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author @baseException
 * @since 2022-11-05
 */

@Slf4j
@Controller
@RequestMapping("/radar")
public class RadarController {
    @Autowired
    private RadarMapper radarMapper;

    @Autowired
    private RadarService radarService;

    @Autowired
    private RedisTemplate redisTemplate;


    @RequestMapping("/curShow")
    public String curShow(Model model, User user) {

        // 检查是否登录
//        if(!checkIsLogin(request)) {
//            return "login";
//        }
        if(user == null) {
            log.info("用户未登录，返回到登录页面");
            return "login";
        }


        QueryWrapper<Radar> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("time");
        queryWrapper.last("limit 1");
        List<Radar> list = radarMapper.selectList(queryWrapper);
        model.addAttribute("curRadars", list);
        return "curRadar";
    }

    @RequestMapping("/queryShow")
    public String queryShow() {
        return "queryRadar";
    }

    @RequestMapping("/historyShow")
    public String historyShow(@RequestParam("start_time") String start_time, @RequestParam("end_time") String end_time,
                              @RequestParam("page") int page,
                              Model model) throws ParseException {


        LocalDateTime time1 = Str2Time.getTime(start_time);
        LocalDateTime time2 = Str2Time.getTime(end_time);

        // pageSize默认为5
        LambdaQueryWrapper<Radar> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Radar::getTime);
        wrapper.gt(Radar::getTime, time1);
        wrapper.le(Radar::getTime, time2);

        int pageSize = 5;

        Page<Radar> pageInfo = new Page<>(page, pageSize);
        List<Radar> records = radarService.page(pageInfo, wrapper).getRecords();

        // 如果查询到当前页的数量为0，就返回第一页的内容
        if(records.size() < 1 && page > 1) {
            records = radarService.page(new Page<Radar>(1, pageSize), wrapper).getRecords();
        }

        model.addAttribute("hisRadars", records);
        return "hisRadar";
    }


    public Boolean checkIsLogin(HttpServletRequest request) {

        // 1. 要登录并且在redis中未失效才放行
        String userTicket = (String)request.getSession().getAttribute("userTicket");

        if(userTicket != null && redisTemplate.hasKey(userTicket)) {
            log.info("用户已登录");
            return true;
        }

        log.info("用户未登录，跳转回登录页面");
        return false;
    }

}
